﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKArchUtils.Utils;
using YJKRevitBase.WinForm;
using YJKElementFinish.ColumnSurface.Controllers;
using YJKElementFinish.ColumnSurface.DataDefinition;
using YJKElementFinish.ColumnSurface.Forms;
using YJKElementFinish.ColumnSurface.Managers;
using YJKRevitView;

namespace YJKElementFinish.ColumnSurface
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdAddSurfaceLayersWithColumn : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			CmdViewType types = CmdViewType.FloorPlan;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			HideWarningAndErroringKit @object = new HideWarningAndErroringKit(new List<string>
			{
				"create wall"
			}, HideWarningAndErroringKit.HideKitEnum.HideErroring);
			cmdData.Application.Application.FailuresProcessing += @object.OnFailuresProcessing;
			if (AddSurfaceLayersController._hWndRevit == null)
			{
				AddSurfaceLayersController._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			Document document = cmdData.Application.ActiveUIDocument.Document;
			List<WallTypeLayerInfos> surfaceLayerWallType = WallTypeManager.GetSurfaceLayerWallType(document);
			SurfaceLayerSettingForm surfaceLayerSettingForm = new SurfaceLayerSettingForm(document, SurfaceLayerSettingForm.FormStyle.SLT_Column, surfaceLayerWallType);
			List<ElementId> list = new List<ElementId>();
			EditRefCurveNewForm.FormOperateStyle formOperateStyle = EditRefCurveNewForm.FormOperateStyle.FOS_Column;
			Result result;
			try
			{
				while (surfaceLayerSettingForm.ShowDialog() == DialogResult.OK)
				{
					if (surfaceLayerSettingForm.ResultStyle != SurfaceLayerSettingForm.FormResultStyle.FRS_Edit)
					{
						new AddSurfaceLayersController(cmdData).AddSurfaceByRefCurve(surfaceLayerSettingForm.CurrentWallTypeLayerInfo);
						RefCurveController.DeleteElements(document, list);
						return Autodesk.Revit.UI.Result.Succeeded;
					}
					RefCurveController refCurveController = new RefCurveController(cmdData, list);
					string text = "";
					refCurveController.EditRefCurve(formOperateStyle, ref list, ref text);
				}
				RefCurveController.DeleteElements(document, list);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Exception)
			{
				RefCurveController.DeleteElements(document, list);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}
	}
}
